//
//  NoodleType.m
//  demo_3
//
//  Created by 才 陈 on 12-4-1.
//  Copyright (c) 2012年 sjtu. All rights reserved.
//

#import "NoodleType.h"

NSDictionary *noodleTypes;

@implementation NoodleType

@synthesize prescription;
@synthesize typeName;
@synthesize displayText;
@synthesize score;

-(id) init:(NSString *)tid :(NSString *) tname :(int)tscore :(NSArray *)ingredients
{
    if(self=[super init])
    {
        typeId = tid;
        typeName = tname;
        score = tscore;
        prescription = ingredients;
    }
    
    return self;
}

/*
 noodle.type.7.id=7
 noodle.type.7.text=黄瓜羊肉刀削面
 noodle.type.7.name=TYPE_CUCUMBER_MUTTON_NOODLE
 noodle.type.7.score=35
 noodle.type.7.prescription=1:10,3:10,5:10
 
 noodle.type.8.id=8
 noodle.type.8.text=萝卜羊肉刀削面
 noodle.type.8.name=TYPE_RADISH_MUTTON_NOODLE
 noodle.type.8.score=40
 noodle.type.8.prescription=1:10,3:10,4:10
 
 noodle.type.9.id=9
 noodle.type.9.text=黄瓜牛肉刀削面
 noodle.type.9.name=TYPE_CUCUMBER_BEEF_NOODLE
 noodle.type.9.score=40
 noodle.type.9.prescription=1:10,2:10,5:10
 */
+(NSDictionary *) noodleTypes:(NSDictionary *)props :(NSDictionary *)ingredientTypes
{
    if(noodleTypes==Nil)
    {
        noodleTypes = [[NSMutableDictionary alloc] init];
        
        NSString *totalStr = [props valueForKey:@"noodle.type.total"];
        int total = [totalStr intValue];
        
        for(int i=1; i<=total; i++)
        {
            NSString *typeIdKey = [NSString stringWithFormat:@"noodle.type.%d.id", i];
            NSString *typeNameKey = [NSString stringWithFormat:@"noodle.type.%d.name", i];
            NSString *typeScoreKey = [NSString stringWithFormat:@"noodle.type.%d.score", i];
            NSString *typePrescriptionKey = [NSString stringWithFormat:@"noodle.type.%d.prescription", i];
            NSString *typeTextKey = [NSString stringWithFormat:@"noodle.type.%d.text", i];
            
            NSString *tid = [props valueForKey:typeIdKey];
            NSString *typeName = [props valueForKey:typeNameKey];
            NSString *typeScore = [props valueForKey:typeScoreKey];
            NSString *prescription = [props valueForKey:typePrescriptionKey];
            NSString *typeText = [props valueForKey:typeTextKey];
            
            int score = [typeScore intValue];
            NSArray *prescriptionList = [Prescription getPrescriptionList:prescription :ingredientTypes];
            
            NoodleType *type = [[NoodleType alloc] init:tid :typeName :score :prescriptionList];
            type.displayText = typeText;
            [noodleTypes setValue:type forKey:tid];
        }
    }
    
    return noodleTypes;
}

+(NSDictionary *) NOODLE_TYPES
{
    if(noodleTypes==Nil) [NSException raise:@"noodle types has not been initilized yet." format:@"variable noodleType should be initialized first.", Nil];
    
    return noodleTypes;
}

+(NoodleType *) nextBowlOfNoodleType:(int)totalScore
{
    if(totalScore<100)
    {
        NSString *dftNoodleTypeId = [NSString stringWithFormat:@"%d", TYPE_BEEF_NOODLE];
        return [[NoodleType NOODLE_TYPES] valueForKey:dftNoodleTypeId];
    }
    else
    {
        int rdmNoodleTypeId = arc4random()%4+5;
        NSString *nxtBowlOfNoodleTypeId = [NSString stringWithFormat:@"%d", rdmNoodleTypeId];
        return [[NoodleType NOODLE_TYPES] valueForKey:nxtBowlOfNoodleTypeId];
    }
}

@end
